package com.atzly.community.controller;

import com.atzly.community.constant.CommentConstant;
import com.atzly.community.entity.Event;
import com.atzly.community.entity.Page;
import com.atzly.community.entity.User;
import com.atzly.community.event.EventProducter;
import com.atzly.community.service.FollowService;
import com.atzly.community.service.UserService;
import com.atzly.community.utils.CommunityUtils;
import com.atzly.community.utils.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 关注业务
 * 补充:系统关注通知
 *
 * @author zlysimida
 * @date 2021/8/31 - 8:42
 */
@Controller
@RequestMapping("/follow")
public class FollowController implements CommentConstant {

    @Autowired
    private FollowService followService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducter producter;

    @PostMapping("/add")
    @ResponseBody
    public String follow(int entityType, int entityId) {
        User user = hostHolder.getUserFromThread();
        followService.follow(user.getId(), entityType, entityId);

        // 当关注时触发关注通知事件
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW_KEY)
                .setUserId(user.getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                // 现在只对人进行关注
                .setEntityUserId(entityId);
        producter.sendMessage(event);

        return CommunityUtils.JsontoString(0, "已关注!");
    }

    @PostMapping("/del")
    @ResponseBody
    public String unFollow(int entityType, int entityId) {
        User user = hostHolder.getUserFromThread();
        followService.unFollow(user.getId(), entityType, entityId);

        return CommunityUtils.JsontoString(0, "已取消关注!");
    }

    // 指定用户所关注的列表
    @GetMapping("/followee/{userId}")
    public String FolloweeList(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.selectById(userId);
        if (user == null) {
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/followee" + userId);
        page.setTotal((int) followService.followeeCount(userId, ENTITY_TYPE_USER));


        List<Map<String, Object>> userList = followService.findFolloweeList(
                userId, page.getOffset(), page.getLimit());
        model.addAttribute("users", userList);

        return "/site/followee";
    }

    // 指定用户下的所有粉丝
    @GetMapping("/follower/{userId}")
    public String FollowerList(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.selectById(userId);
        if (user == null) {
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/follower" + userId);
        // 粉丝总数
        page.setTotal((int) followService.followerCount(ENTITY_TYPE_USER, userId));

        List<Map<String, Object>> userList = followService.findFollowerList(userId, page.getOffset(), page.getLimit());

        model.addAttribute("users", userList);
        return "/site/follower";
    }


}
